class BlogsController < ApplicationController
  before_action :authenticate_user!, only: [:new, :me_blogs]

  def index
    @blogs = Blog.page(params[:page] || 1).per_page(params[:per_page] || 10).order("id desc")
  end

  def new
    @blog = Blog.new
  end

  def me_blogs
    # @blogs = current_user.blogs.page(params[:page] || 1).per_page(params[:per_page] || 10).order("id desc")
    # @blogs = Blog.where().page(params[:page] || 1).per_page(params[:per_page] || 10).order("id desc")
    logger = Logger.new(STDOUT)
    logger.info current_user
    @blogs = Blog.page(params[:page] || 1).per_page(params[:per_page] || 10).where(user_id: current_user.id).order("id desc")
  end

  def create
    @blog = Blog.new(blog_attrs)
    @blog.user = current_user
    if @blog.save
      flash[:notice] = "博客创建成功"
      redirect_to blogs_path
    else
      flash[:notice] = "博客创建失败"
      render action: :new
    end
  end

  def show
    @blog = Blog.find params[:id]
    @comments = Comment.where(blog_id: @blog.id).order "id desc"
  end

  private

  def blog_attrs
    params.require(:blog).permit(:title, :content)
  end

end
